package com.yzh.sharding.config;

import com.yzh.sharding.service.HandlerService;
import com.yzh.sharding.service.HandlerService2;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;

import java.util.Map;

/**
 * @program: yzh->HandlerContext
 * @description: 处理器上下文
 * @author: yangzhanghui
 * @create: 2019-11-29 15:31
 **/
@AllArgsConstructor
public class HandlerContext {

    private Map<Integer, Class> handlerMap;

    @SneakyThrows
    public HandlerService getHandlerInstance(Integer type){
        Class clazz = handlerMap.get(type);
        if (clazz == null) {throw new IllegalArgumentException("not found handler for type: " + type);}
        HandlerService handlerService = SpringGainContextUtils.getBean(clazz.getSimpleName());
        if (handlerService==null) {throw new Exception("获取的类不存在");}
        return handlerService;
    }

    @SneakyThrows
    public HandlerService2 getHandler2Instance(Integer type){
        Class clazz = handlerMap.get(type);
        if (clazz == null) {throw new IllegalArgumentException("not found handler for type: " + type);}
        HandlerService2 handlerService2 = SpringGainContextUtils.getBean(clazz.getSimpleName());
        if (handlerService2==null) {throw new Exception("获取的类不存在");}
        return handlerService2;
    }
}
